using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;

namespace PropertyExpression.ControllableQuery.Toolkit
{
    public interface IChoiceInvocationContext
    {
        IEnumerable<QueryCreator> GetMethodBaseInvocations(ParameterInfo[] parameterInfos, Func<object[], object> invokeFunc);
    }

    public static class ChoiceInvocationContextExtensions
    {
        public static IEnumerable<QueryCreator> GetConstructorInvocations(this IChoiceInvocationContext it, Type type)
        {
            return type.GetConstructors(ControllableQuery.Util.AllBindingFlags).SelectMany(
                constructorInfo => it.GetMethodBaseInvocations(constructorInfo.GetParameters(), constructorInfo.Invoke));
        }

        public static IEnumerable<QueryCreator> GetStaticMethodInvocations(this IChoiceInvocationContext it, MethodInfo methodInfo)
        {
            return it.GetMethodBaseInvocations(methodInfo.GetParameters(), args => methodInfo.Invoke(null, args));
        }
    }
}